﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace USERS.CYT369.COM.Models
{
    public static class JObjectExtention
    {
        public static Newtonsoft.Json.Linq.JValue GetJValue(this Newtonsoft.Json.Linq.JObject query, string name)
        {
            if (query != null && !string.IsNullOrEmpty(name))
            {
                return query.GetValue(name) as Newtonsoft.Json.Linq.JValue;
            }

            return null;
        }

        public static Newtonsoft.Json.Linq.JArray GetJaValue(this Newtonsoft.Json.Linq.JObject query, string name)
        {
            if (query != null && !string.IsNullOrEmpty(name))
            {
                return query.GetValue(name) as Newtonsoft.Json.Linq.JArray;
            }

            return null;
        }


        public static List<T> GetArray<T>(this Newtonsoft.Json.Linq.JObject query, string name, Func<Newtonsoft.Json.Linq.JToken, T> action)
        {
            var objArray = query.GetJaValue(name);
            if (objArray != null)
            {

                var list = new List<T>();
                foreach (var item in objArray)
                {
                    var t = action(item);
                    if (t != null)
                    {
                        list.Add(t);
                    }
                }

                return list;
            }

            return null;
        }

        public static List<T> GetArray<T>(this Newtonsoft.Json.Linq.JObject query, string name, Action<Newtonsoft.Json.Linq.JToken, T> action) where T : class, new()
        {
            var objArray = query.GetJaValue(name);
            if (objArray != null)
            {
                var list = new List<T>();
                foreach (var item in objArray)
                {
                    var t = new T();
                    action(item, t);
                    list.Add(t);
                }

                return list;
            }

            return null;
        }

        public static T GetObject<T>(this Newtonsoft.Json.Linq.JObject query, string name, Action<Newtonsoft.Json.Linq.JToken, T> action) where T : class, new()
        {
            if (query != null)
            {
                var jToken = query.GetValue(name);
                if (jToken != null)
                {
                    var t = new T();
                    action(jToken, t);
                    return t;
                }
            }

            return null;
        }

        public static void GetObject(this Newtonsoft.Json.Linq.JObject query, string name, Action<Newtonsoft.Json.Linq.JToken> action)
        {
            if (query != null)
            {
                var jToken = query.GetValue(name);
                if (jToken != null)
                {
                    action(jToken);
                }
            }
        }

        public static string GetString(this Newtonsoft.Json.Linq.JObject query, string name)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.String)
                {
                    return jv.Value as string;
                }
                else if (jv.Value != null)
                {
                    return jv.Value.ToString();
                }
            }

            return null;
        }

        public static char? GetChar(this Newtonsoft.Json.Linq.JObject query, string name)
        {
            string str = null;
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.String)
                {
                    str = jv.Value as string;
                }
                else if (jv.Value != null)
                {
                    str = jv.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(str))
            {
                return null;
            }
            else
            {
                return str.ElementAt(0);
            }
        }

        public static char GetChar(this Newtonsoft.Json.Linq.JObject query, string name, char defaultValue)
        {
            string str = null;
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.String)
                {
                    str = jv.Value as string;
                }
                else if (jv.Value != null)
                {
                    str = jv.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(str))
            {
                return defaultValue;
            }
            else
            {
                return str.ElementAt(0);
            }
        }

        public static int[] GetIntegerArray(this Newtonsoft.Json.Linq.JObject query, string name, char[] separator)
        {
            return GetIntegerList(query, name, separator).ToArray();
        }

        public static List<int> GetIntegerList(this Newtonsoft.Json.Linq.JObject query, string name, char[] separator)
        {
            var array = new List<int>();

            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.String)
                {
                    string str = jv.Value as string;
                    if (!string.IsNullOrEmpty(str))
                    {
                        var sps = str.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                        int num = 0;
                        foreach (var s in sps)
                        {
                            if (int.TryParse(s, out num))
                            {
                                array.Add(num);
                            }
                        }
                    }
                }
            }

            return array;
        }


        public static int? GetInteger(this Newtonsoft.Json.Linq.JObject query, string name)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Integer)
                {
                    try
                    {
                        return (int)jv.Value;
                    }
                    catch (System.InvalidCastException)
                    {
                        int num = 0;
                        if (int.TryParse(jv.Value.ToString(), out num))
                        {
                            return num;
                        }
                    }
                }
                else if (jv.Value != null)
                {
                    int num = 0;
                    if (int.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static int GetInteger(this Newtonsoft.Json.Linq.JObject query, string name, int defaultValue)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Integer)
                {
                    try
                    {
                        return (int)jv.Value;
                    }
                    catch (System.InvalidCastException)
                    {
                        int num = 0;
                        if (int.TryParse(jv.Value.ToString(), out num))
                        {
                            return num;
                        }
                    }
                }
                else if (jv.Value != null)
                {
                    int num = 0;
                    if (int.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static long? GetLong(this Newtonsoft.Json.Linq.JObject query, string name)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Integer)
                {
                    return (long)jv.Value;
                }
                else if (jv.Value != null)
                {
                    long num = 0;
                    if (long.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static long GetLong(this Newtonsoft.Json.Linq.JObject query, string name, long defaultValue)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Integer)
                {
                    return (long)jv.Value;
                }
                else if (jv.Value != null)
                {
                    long num = 0;
                    if (long.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static float? GetFloat(this Newtonsoft.Json.Linq.JObject query, string name)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Integer || jv.Type == Newtonsoft.Json.Linq.JTokenType.Float)
                {
                    return (float)jv.Value;
                }
                else if (jv.Value != null)
                {
                    float num = 0;
                    if (float.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static float GetFloat(this Newtonsoft.Json.Linq.JObject query, string name, float defaultValue)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Integer || jv.Type == Newtonsoft.Json.Linq.JTokenType.Float)
                {
                    return (float)jv.Value;
                }
                else if (jv.Value != null)
                {
                    float num = 0;
                    if (float.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static double? GetDouble(this Newtonsoft.Json.Linq.JObject query, string name)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Integer || jv.Type == Newtonsoft.Json.Linq.JTokenType.Float)
                {
                    return (double)jv.Value;
                }
                else if (jv.Value != null)
                {
                    double num = 0;
                    if (double.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static double GetDouble(this Newtonsoft.Json.Linq.JObject query, string name, double defaultValue)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Integer || jv.Type == Newtonsoft.Json.Linq.JTokenType.Float)
                {
                    return (double)jv.Value;
                }
                else if (jv.Value != null)
                {
                    double num = 0;
                    if (double.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static byte? GetByte(this Newtonsoft.Json.Linq.JObject query, string name)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Bytes || jv.Type == Newtonsoft.Json.Linq.JTokenType.Integer)
                {
                    return (byte)jv.Value;
                }
                else if (jv.Value != null)
                {
                    byte num = 0;
                    if (byte.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static byte GetByte(this Newtonsoft.Json.Linq.JObject query, string name, byte defaultValue)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Bytes || jv.Type == Newtonsoft.Json.Linq.JTokenType.Integer)
                {
                    return (byte)jv.Value;
                }
                else if (jv.Value != null)
                {
                    byte num = 0;
                    if (byte.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }


        public static DateTime? GetDateTime(this Newtonsoft.Json.Linq.JObject query, string name)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Date)
                {
                    return (DateTime)jv.Value;
                }
                else if (jv.Value != null)
                {
                    DateTime num = DateTime.Now;
                    if (DateTime.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static DateTime GetDateTime(this Newtonsoft.Json.Linq.JObject query, string name, DateTime defaultValue)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Date)
                {
                    return (DateTime)jv.Value;
                }
                else if (jv.Value != null)
                {
                    DateTime num = DateTime.Now;
                    if (DateTime.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static decimal? GetDecimal(this Newtonsoft.Json.Linq.JObject query, string name)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Integer || jv.Type == Newtonsoft.Json.Linq.JTokenType.Float || jv.Type == Newtonsoft.Json.Linq.JTokenType.Bytes)
                {
                    try
                    {
                        return (decimal)jv.Value;
                    }
                    catch (System.InvalidCastException)
                    {
                        decimal d = 0;
                        if (decimal.TryParse(jv.Value.ToString(), out d))
                        {
                            return d;
                        }
                    }
                }
                else if (jv.Value != null)
                {
                    decimal num = 0;
                    if (decimal.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static decimal GetDecimal(this Newtonsoft.Json.Linq.JObject query, string name, decimal defaultValue)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Integer || jv.Type == Newtonsoft.Json.Linq.JTokenType.Float || jv.Type == Newtonsoft.Json.Linq.JTokenType.Bytes)
                {
                    try
                    {
                        return (decimal)jv.Value;
                    }
                    catch (System.InvalidCastException)
                    {
                        decimal d = 0;
                        decimal.TryParse(jv.Value.ToString(), out d);
                        return d;
                    }
                }
                else if (jv.Value != null)
                {
                    decimal num = 0;
                    if (decimal.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static bool? GetBoolean(this Newtonsoft.Json.Linq.JObject query, string name)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Boolean)
                {
                    return (bool)jv.Value;
                }
                else if (jv.Value != null)
                {
                    bool num = false;
                    if (bool.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static bool GetBoolean(this Newtonsoft.Json.Linq.JObject query, string name, bool defaultValue)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Boolean)
                {
                    return (bool)jv.Value;
                }
                else if (jv.Value != null)
                {
                    bool num = false;
                    if (bool.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static Guid? GetGuid(this Newtonsoft.Json.Linq.JObject query, string name)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Guid)
                {
                    return (Guid)jv.Value;
                }
                else if (jv.Value != null)
                {
                    Guid num = Guid.Empty;
                    if (Guid.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static Guid GetGuid(this Newtonsoft.Json.Linq.JObject query, string name, Guid defaultValue)
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Guid)
                {
                    return (Guid)jv.Value;
                }
                else if (jv.Value != null)
                {
                    Guid num = Guid.Empty;
                    if (Guid.TryParse(jv.Value.ToString(), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static T GetEnumFromString<T>(this Newtonsoft.Json.Linq.JObject query, string name, T defaultValue) where T : struct, IConvertible
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.String)
                {
                    T result = defaultValue;
                    if (Enum.TryParse<T>(jv.Value as string, out result))
                    {
                        return result;
                    }
                }
            }

            return defaultValue;
        }

        public static T GetEnumFromString<T>(this Newtonsoft.Json.Linq.JObject query, string name, T defaultValue, bool ignoreCase) where T : struct, IConvertible
        {
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.String)
                {
                    T result = defaultValue;
                    if (Enum.TryParse<T>(jv.Value as string, ignoreCase, out result))
                    {
                        return result;
                    }
                }
            }

            return defaultValue;
        }

        public static T GetEnumFromInteger<T>(this Newtonsoft.Json.Linq.JObject query, string name, T defaultValue) where T : struct, IConvertible
        {
            T result = defaultValue;
            var jv = query.GetJValue(name);
            if (jv != null)
            {
                if (jv.Type == Newtonsoft.Json.Linq.JTokenType.Integer || jv.Type == Newtonsoft.Json.Linq.JTokenType.Bytes)
                {
                    if (jv.Value != null)
                    {
                        result = (T)jv.Value;
                    }
                }
            }

            return result;
        }

    }

    public static class JTokenExtention
    {
        public static List<T> GetArray<T>(this Newtonsoft.Json.Linq.JToken query, string name, Func<Newtonsoft.Json.Linq.JToken, T> action)
        {
            var obj = query as Newtonsoft.Json.Linq.JObject;
            if (obj != null)
            {
                return obj.GetArray<T>(name, action);
            }

            return null;
        }

        public static string GetString(this Newtonsoft.Json.Linq.JToken query, string name)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<string>(name);
                }
                catch
                {
                    return null;
                }
            }
            return null;
        }

        public static char? GetChar(this Newtonsoft.Json.Linq.JToken query, string name)
        {
            string str = null;
            var jv = query.GetString(name);
            if (string.IsNullOrEmpty(str))
            {
                return null;
            }
            else
            {
                return str.ElementAt(0);
            }
        }

        public static char GetChar(this Newtonsoft.Json.Linq.JToken query, string name, char defaultValue)
        {
            string str = null;
            var jv = query.GetString(name);

            if (string.IsNullOrEmpty(str))
            {
                return defaultValue;
            }
            else
            {
                return str.ElementAt(0);
            }
        }

        public static int[] GetIntegerArray(this Newtonsoft.Json.Linq.JToken query, string name, char[] separator)
        {
            return GetIntegerList(query, name, separator).ToArray();
        }

        public static List<int> GetIntegerList(this Newtonsoft.Json.Linq.JToken query, string name, char[] separator)
        {
            var array = new List<int>();

            var jv = query.GetString(name);
            if (jv != null)
            {
                string str = jv;
                if (!string.IsNullOrEmpty(str))
                {
                    var sps = str.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                    int num = 0;
                    foreach (var s in sps)
                    {
                        if (int.TryParse(s, out num))
                        {
                            array.Add(num);
                        }
                    }
                }
            }

            return array;
        }


        public static int? GetInteger(this Newtonsoft.Json.Linq.JToken query, string name)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<int>(name);
                }
                catch
                {
                    int num = 0;
                    if (int.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static int GetInteger(this Newtonsoft.Json.Linq.JToken query, string name, int defaultValue)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<int>(name);
                }
                catch
                {
                    int num = 0;
                    if (int.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static long? GetLong(this Newtonsoft.Json.Linq.JToken query, string name)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<long>(name);
                }
                catch
                {
                    long num = 0;
                    if (long.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static long GetLong(this Newtonsoft.Json.Linq.JToken query, string name, long defaultValue)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<long>(name);
                }
                catch
                {
                    long num = 0;
                    if (long.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static float? GetFloat(this Newtonsoft.Json.Linq.JToken query, string name)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<float>(name);
                }
                catch
                {
                    float num = 0;
                    if (float.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static float GetFloat(this Newtonsoft.Json.Linq.JToken query, string name, float defaultValue)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<float>(name);
                }
                catch
                {
                    float num = 0;
                    if (float.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static double? GetDouble(this Newtonsoft.Json.Linq.JToken query, string name)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<double>(name);
                }
                catch
                {
                    double num = 0;
                    if (double.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static double GetDouble(this Newtonsoft.Json.Linq.JToken query, string name, double defaultValue)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<double>(name);
                }
                catch
                {
                    double num = 0;
                    if (double.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static byte? GetByte(this Newtonsoft.Json.Linq.JToken query, string name)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<byte>(name);
                }
                catch
                {
                    byte num = 0;
                    if (byte.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static byte GetByte(this Newtonsoft.Json.Linq.JToken query, string name, byte defaultValue)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<byte>(name);
                }
                catch
                {
                    byte num = 0;
                    if (byte.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }


        public static DateTime? GetDateTime(this Newtonsoft.Json.Linq.JToken query, string name)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<DateTime>(name);
                }
                catch
                {
                    DateTime num = DateTime.Now;
                    if (DateTime.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static DateTime GetDateTime(this Newtonsoft.Json.Linq.JToken query, string name, DateTime defaultValue)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<DateTime>(name);
                }
                catch
                {
                    DateTime num = DateTime.Now;
                    if (DateTime.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static decimal? GetDecimal(this Newtonsoft.Json.Linq.JToken query, string name)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<decimal>(name);
                }
                catch
                {
                    decimal num = 0;
                    if (decimal.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static decimal GetDecimal(this Newtonsoft.Json.Linq.JToken query, string name, decimal defaultValue)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<decimal>(name);
                }
                catch
                {
                    decimal num = 0;
                    if (decimal.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static bool? GetBoolean(this Newtonsoft.Json.Linq.JToken query, string name)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<bool>(name);
                }
                catch
                {
                    bool num = false;
                    if (bool.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static bool GetBoolean(this Newtonsoft.Json.Linq.JToken query, string name, bool defaultValue)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<bool>(name);
                }
                catch
                {
                    bool num = false;
                    if (bool.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static Guid? GetGuid(this Newtonsoft.Json.Linq.JToken query, string name)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<Guid>(name);
                }
                catch
                {
                    Guid num = Guid.Empty;
                    if (Guid.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return null;
        }

        public static Guid GetGuid(this Newtonsoft.Json.Linq.JToken query, string name, Guid defaultValue)
        {
            if (query.HasValues)
            {
                try
                {
                    return query.Value<Guid>(name);
                }
                catch
                {
                    Guid num = Guid.Empty;
                    if (Guid.TryParse(query.GetString(name), out num))
                    {
                        return num;
                    }
                }
            }

            return defaultValue;
        }

        public static T GetEnumFromString<T>(this Newtonsoft.Json.Linq.JToken query, string name, T defaultValue) where T : struct, IConvertible
        {
            if (query.HasValues)
            {
                try
                {
                    T result = defaultValue;
                    if (Enum.TryParse<T>(query.GetString(name), out result))
                    {
                        return result;
                    }
                }
                catch
                {

                }
            }

            return defaultValue;
        }

        public static T GetEnumFromString<T>(this Newtonsoft.Json.Linq.JToken query, string name, T defaultValue, bool ignoreCase) where T : struct, IConvertible
        {
            if (query.HasValues)
            {
                try
                {
                    T result = defaultValue;
                    if (Enum.TryParse<T>(query.GetString(name), ignoreCase, out result))
                    {
                        return result;
                    }
                }
                catch
                {

                }
            }

            return defaultValue;
        }

        public static T GetEnumFromInteger<T>(this Newtonsoft.Json.Linq.JToken query, string name, T defaultValue) where T : struct, IConvertible
        {
            T result = defaultValue;

            if (query.HasValues)
            {
                try
                {
                    var jv = query.GetInteger(name);
                    if (jv.HasValue)
                    {
                        result = (T)(object)jv.Value;
                    }
                }
                catch
                {

                }
            }

            return result;
        }

    }
}
